from bencoder import bdecode
import codecs


def _get_single_filename(meta_info):
    info = meta_info[b"info"]
    if b"name.utf-8" in info:
        filename = info[b"name.utf-8"]
    else:
        filename = info[b"name"]

    for c in filename:
        if c == "'":
            filename = filename.replace(c, "\\'")
    return filename.decode()


def _get_multi_filename(meta_info):
    """
    获取种子多个文件名
    """
    files = meta_info[b"info"][b"files"]
    info = []
    for item in files:
        for k, v in item.items():
            if isinstance(v, list):
                try:
                    v = [i.decode() for i in v]
                except:
                    continue
            elif isinstance(v, int):
                v = round(v / 1024 / 1024, 2)
            else:
                v = codecs.getencoder("hex")(v)[0].decode()
            info.append((k.decode(), v))
    return info

def parse():
    with open('liuyue.torrent', "rb") as f:
        meta_info =  bdecode(f.read())
    return _get_multi_filename(meta_info)




info = parse()
print(info)